package com.neusmart.common.fragment;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import com.neusmart.common.R;
import com.neusmart.common.dialog.CustomLoadingDialog;
import com.neusmart.common.dialog.HintDialog;
import com.neusmart.common.dialog.NewAlertDialog;
import com.neusmart.common.dialog.NewAlertDialogWithTitle;
import com.neusmart.common.dialog.OnHintListener;
import com.neusmart.common.dialog.OnNewClickListener;
import com.neusmart.common.util.MToast;
import com.neusmart.common.util.NetworkUtil;

/**
 * base fragment
 * <p/>
 * roy
 */
public abstract class BaseFragment extends Fragment implements DialogInterface.OnCancelListener {

    protected Context mContext;
    protected View mContentView;
    //
    private CustomLoadingDialog loadingDialog;

    private boolean currentNetworkConnected = true;

    private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            boolean connected = isNetworkConnected();
            if (currentNetworkConnected) {
                if (!connected)
                    showNetworkConnection(false);
            } else {
                if (connected) {
                    showNetworkConnection(true);
                    onRefreshContent();
                }
            }
            currentNetworkConnected = connected;
        }
    };

    protected boolean showNetworkConnection(boolean isConnected) {
        showToast(isConnected ? R.string.network_connect : R.string.network_interupt);
        return false;
    }

    protected void onRefreshContent() {
        //网络连接时，刷新页面
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerReceiver();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContentView = inflater.inflate(getLayoutResID(), null);
        return mContentView;
    }

    protected abstract int getLayoutResID();

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
    }

    protected abstract void init();

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver();
    }

    private void registerReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        if (null != receiver)
            mContext.registerReceiver(receiver, filter);
    }

    private void unregisterReceiver() {
        if (null != receiver)
            mContext.unregisterReceiver(receiver);
    }

    protected View findViewById(int id) {
        return mContentView.findViewById(id);
    }

    protected boolean isNetworkConnected() {
        return NetworkUtil.isNetworkConnected(mContext);
    }

    protected void showLoadingDialog() {
        loadingDialog = new CustomLoadingDialog(mContext);
        loadingDialog.show();
    }

    protected void showLoadingDialog(String txt) {
        loadingDialog = new CustomLoadingDialog(mContext);
        loadingDialog.setHintText(txt);
        loadingDialog.show();
    }

    protected void showLoadingDialog(int txtId) {
        loadingDialog = new CustomLoadingDialog(mContext);
        loadingDialog.setHintText(txtId);
        loadingDialog.show();
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        dismissLoadingDialog();
    }

    protected void dismissLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing())
            loadingDialog.dismiss();
    }

    protected void showToast(int resId) {
        MToast.showText(mContext, resId);
    }

    protected void showToast(String msg) {
        MToast.showText(mContext, msg);
    }

    protected void showHintDialog(int resId, OnHintListener l) {
        this.showHintDialog(getString(resId), l);
    }

    protected void showHintDialog(String text, OnHintListener l) {
        HintDialog dialog = new HintDialog(mContext);
        dialog.setMessage(text);
        dialog.setOnHintListener(l);
        dialog.show();
    }

    protected void showNewAlertDialog(String msg, String btnLeft, String btnRight, OnNewClickListener l) {
        NewAlertDialog dialog = new NewAlertDialog(mContext);
        dialog.setMessage(msg);
        dialog.setLeftButton(btnLeft);
        dialog.setRightButton(btnRight);
        dialog.setOnNewClickListener(l);
        dialog.show();
    }

    protected void showNewAlertDialog(int msgTextId, int leftTextId, int rightTextId, OnNewClickListener l) {
        NewAlertDialog dialog = new NewAlertDialog(mContext);
        dialog.setMessage(msgTextId);
        dialog.setLeftButton(leftTextId);
        dialog.setRightButton(rightTextId);
        dialog.setOnNewClickListener(l);
        dialog.show();
    }

    protected void showNewAlertDialogWithTitle(String title, String msg, String btnLeft, String btnRight, OnNewClickListener l) {
        NewAlertDialogWithTitle dialog = new NewAlertDialogWithTitle(mContext);
        dialog.setTitle(title);
        dialog.setMessage(msg);
        dialog.setLeftButton(btnLeft);
        dialog.setRightButton(btnRight);
        dialog.setOnNewClickListener(l);
        dialog.show();
    }

    protected void switchActivity(Class<?> cls, Bundle extras) {
        Intent intent = new Intent(mContext, cls);
        if (extras != null)
            intent.putExtras(extras);
        mContext.startActivity(intent);
    }

    protected void switchActivityForResult(Class<?> cls, int requestCode, Bundle extras) {
        Intent intent = new Intent(mContext, cls);
        if (extras != null)
            intent.putExtras(extras);
        startActivityForResult(intent, requestCode);
    }

    protected void hideSoftInput() {
        ((InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(((Activity) mContext).getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

}
